﻿using System;
using System.Linq;
using System.Windows;
using System.Xml;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;

namespace Oxage.Controls.AvalonEditExtensions
{
	public abstract class SyntaxHighlighterBase : ISyntaxHighlighter
	{
		#region ISyntaxHighlighter Members
		public abstract string Name
		{
			get;
		}

		public abstract string[] FileExtensions
		{
			get;
		}

		public string GetDialogFilter()
		{
			string defaultFileExtension = (this.FileExtensions != null ? this.FileExtensions.FirstOrDefault() : ".*");
			return string.Format("{0} (*{1})|*{1}", this.Name, defaultFileExtension);
		}

		protected abstract string XshdPath
		{
			get;
		}

		public virtual IHighlightingDefinition GetDefinition()
		{
			IHighlightingDefinition gCodeHighlighting;
			var info = Application.GetResourceStream(new Uri(this.XshdPath, UriKind.Relative));

			if (info == null || info.Stream == null)
			{
				throw new ArgumentNullException("stream", "Could not find embedded resource!");
			}

			using (XmlReader reader = new XmlTextReader(info.Stream))
			{
				gCodeHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
				return gCodeHighlighting;
			}
		}

		public virtual bool IsBreakpointAllowed(string text)
		{
			return true;
		}
		#endregion
	}
}
